<?php
/*
 * Copyright (c) 2011 Todd Brady
 *
 * PS3Theatre is freely distributable under the terms of the MIT license.
 * For details, see the MIT-Liscence web site: http://www.opensource.org/licenses/mit-license.php 
 */
 
	include("includes/db_connect.php");
	$seriesId = $_GET['seriesId'];
	$seriesName = $_GET['seriesName'];
	$h = $_GET['h'];
	$w = $_GET['w'];
	$columnWidth = ceil(($w/3)+($w/6));
	
	$seriesInfoResult=mysql_query("SELECT s.tvdbseries_id, s.description, s.genre, ci.subfolder_path, ci.filename FROM tbl_tvshow_series s LEFT OUTER JOIN tbl_cacheimage ci ON (s.tvdbseries_id = ci.link_id and ci.type_id=2 and ci.keyword='backdrop') WHERE s.id=$seriesId") or die(mysql_error());
	$seriesRow = mysql_fetch_array($seriesInfoResult);
	$seasonsResult=mysql_query("SELECT e.season,s.tvdbseries_id, count(*) as epCount FROM tbl_tvshow_episode e, tbl_tvshow_series s WHERE s.id=e.series_id AND e.series_id=$seriesId and e.season > 0 GROUP BY e.season ORDER BY e.season") or die(mysql_error());
	$specialsResult=mysql_query("SELECT e.season,s.tvdbseries_id, count(*) as epCount FROM tbl_tvshow_episode e, tbl_tvshow_series s WHERE s.id=e.series_id AND e.series_id=$seriesId and e.season = 0 GROUP BY e.season ORDER BY e.season") or die(mysql_error());
	
	$numCols = 1;
	$numCols+=mysql_num_rows($seasonsResult);
	$numCols+=mysql_num_rows($specialsResult);
	if($numCols > 2){
		$numCols++;
	}
	
	//variables for the top left info panel
	$leftTopLbl1 = 'TV Shows';
	$leftTopLbl2 = $seriesName;
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PS3 Theatre <?php echo $_SESSION['version'];?></title>
<script type="text/javascript" src="js/time.js"></script>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/animator.js"></script>
<script type="text/javascript" src="js/ui.js"></script>
<script type="text/javascript" src="js/tvShowsSeasons_ui.js"></script>
<script>
	var h = <?php echo $h;?>;
	var w = <?php echo $w;?>;
	var columnWidth = <?php echo $columnWidth;?>;

	var navBarIndex = 1;
	var navBarItems = <?php echo $numCols;?>;

	function selectSeason(index)
	{
		var td = $("menuTD"+index);
		
		seriesId = td.readAttribute('seriesId');
		season = td.readAttribute('season');
		epCount = td.readAttribute('epCount');
	
		var seasonString = '';

		loadSeasonPoster(seriesId, season);
		
		if(season == '..'){
			$('subInfoDiv').addClassName('hide');
		}else{
			$('subInfoDiv').removeClassName('hide');
		}
		
		if(season == 'all'){
			seasonString = 'All Episodes';
		}else if(season == '..'){
			seasonString = '';
			numberEps = '';
		}else if(season == '0'){
			seasonString = 'Specials';
		}else{
			seasonString = season;
		}
		
		$('seasonDescSpan').innerHTML = seasonString;
		$('episodeDescSpan').innerHTML = epCount;
	}
	
	function loadBackgroundArt(seriesId) {
		glSeriesId = seriesId;
		new Ajax.Request('scripts/tvShowImport.php?seriesId='+seriesId+'&backdrop=Y&uId='+getUniqueCode(),
		  {
			method:'get',
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON();
				
				if(json.backdropSrc != ''){
					document.body.background=json.backdropSrc;
				}else{
					document.body.background='images/movies-background.png';
				}
			},
			onFailure: function(){ alert('error loading backdrop...'); }
		  });
	 }
	 
	function loadSeasonPoster(seriesId, seasonId){
		if(seasonId == '..'){
			$('seasonPosterDIV').innerHTML='<img src="images/tvSeason-upposter.png" height="578" width="400"/>';
		}else if(seasonId == 'all'){
			$('seasonPosterDIV').innerHTML='<img src="images/tvSeason-allposter.png" height="578" width="400"/>';
		}else{
			new Ajax.Request('scripts/tvShowImport.php?seriesId='+seriesId+'&seasonId='+seasonId+'&seasonPoster=Y&uId='+getUniqueCode(),
			  {
				method:'get',
				onSuccess: function(transport){
					var json = transport.responseText.evalJSON();

					if(json.seasonPosterSrc != ''){
						$('seasonPosterDIV').innerHTML='<img src="'+json.seasonPosterSrc+'" height="578" width="400"/>';
					}else{
						$('seasonPosterDIV').innerHTML='';
					}
				},
				onFailure: function(){ alert('error loading season poster...'); }
			  });
		  }
	}
</script>
<link href="css/main.css" rel="stylesheet" type="text/css">
<meta http-equiv="Pragma" content="no-cache"> 
</head>

<body bgcolor="#000000" background="<?php if($seriesRow['filename'] != ''){ echo $_SESSION['cacheHttpDir'].$seriesRow['subfolder_path'].$seriesRow['filename'];}?>" style="overflow:hidden;" onLoad="clock();<?php if($seriesRow['filename'] == ''){?>loadBackgroundArt(<?php echo $seriesRow['tvdbseries_id'];?>);<?php } ?>selectSeason(1);">
<div class="wrapper" >

<!-- left info panel -->
<?php include('/includes/topLeftInfoPanel.php'); ?>

<!-- right info panel -->
<?php include('/includes/topRightInfoPanel.php'); ?>

<!-- season text scroll panel-->
<div id="navMenu" style="position:absolute; top:80px; width:<?php echo $numCols*$columnWidth;?>px;">
<table width="<?php echo $numCols*$columnWidth;?>" cellpadding="0" cellspacing="0" style="margin-left:-<?php echo $columnWidth/2;?>px;">
<tr>
<!-- up dir col -->
<td id="menuTD0" valign="top" seriesId="<?php echo $seriesRow['tvdbseries_id'];?>" season=".." epCount="0" link="tvShows.php?w=<?php echo $w;?>&h=<?php echo $h;?>&uId=<?php echo time();?>" style="text-align:center;" height="155" width="<?php echo $columnWidth;?>"><span id="menuSpan0" class="tvSeasonLink" style="white-space:nowrap;">..</span></td>

<!-- seasons cols -->
<?php
$i=1;
$totalNumEps = 0;
	
while ($row = mysql_fetch_array($seasonsResult)) {
	$totalNumEps = $totalNumEps + $row['epCount'];

	if($i == 1){
		$initialSeason = $row['season'];
		$initialEpCount = $row['epCount'];
	}
		
	if($row['season'] == 0){
		$name = 'SPECIALS';
	}else{
		$name = 'SEASON '.$row['season'];
	}
	
	$url = 'tvShowsEps.php?seriesId='.$seriesId.'&seriesName='.$seriesName.'&seasonNum='.$row['season'].'&w='.$w.'&h='.$h.'&uId='.time();
?>
	<td id="menuTD<?php echo $i;?>" valign="top" seriesId="<?php echo $seriesRow['tvdbseries_id'];?>" season="<?php echo $row['season'];?>" epCount="<?php echo $row['epCount'];?>" link="<?php echo $url;?>" style="text-align:center;" width="<?php echo $columnWidth;?>"><span id="menuSpan<?php echo $i;?>" class="tvSeasonLink<?php if($i==1){echo 'Over';}?>" style="white-space:nowrap;"><?php echo $name;?></span></td>
<?php
	$i++;
}
?>

<!-- specials cols -->
<?php
while ($row = mysql_fetch_array($specialsResult)) {
	$totalNumEps = $totalNumEps + $row['epCount'];
	
	if($i == 1){
		$initialSeason = $row['season'];
		$initialEpCount = $row['epCount'];
	}
	
	if($row['season'] == 0){
		$name = 'SPECIALS';
	}else{
		$name = 'SEASON '.$row['season'];
	}
	
	$url = 'tvShowsEps.php?seriesId='.$seriesId.'&seriesName='.$seriesName.'&seasonNum='.$row['season'].'&w='.$w.'&h='.$h.'&uId='.time();
?>
<td id="menuTD<?php echo $i;?>" valign="top" seriesId="<?php echo $seriesRow['tvdbseries_id'];?>" season="<?php echo $row['season'];?>" epCount="<?php echo $row['epCount'];?>" link="<?php echo $url;?>" style="text-align:center;" width="<?php echo $columnWidth;?>"><span id="menuSpan<?php echo $i;?>" class="tvSeasonLink<?php if($i==1){echo 'Over';}?>" style="white-space:nowrap;"><?php echo $name;?></span></td>
<?php 
$i++;
}
?>

<!-- all ep col -->
<?php 
if($numCols > 2){
	$name = 'ALL EPS';
	$url = 'tvShowsEps.php?seriesId='.$seriesId.'&seriesName='.$seriesName.'&seasonNum=all&w='.$w.'&h='.$h.'&uId='.time();
?>
<td id="menuTD<?php echo $i;?>" valign="top" seriesId="<?php echo $seriesRow['tvdbseries_id'];?>" season="all" epCount="<?php echo $totalNumEps;?>" link="<?php echo $url;?>" style="text-align:center;" width="<?php echo $columnWidth;?>"><span id="menuSpan<?php echo $i;?>" class="tvSeasonLink<?php if($i==1){echo 'Over';}?>" style="white-space:nowrap;"><?php echo $name; ?></span></td>
<?php
}
?>
</tr>
</table>
</div>

<!-- sub info div -->
<div id="subInfoDiv" style="position:absolute; top:900px; left:25px; z-index:10;">
<table>
<tr>
<td nowrap="nowrap">
<span class="seasonDescHeader">
Season:
</span>
<span id="seasonDescSpan" class="seasonDescText">
4
</span>
</td>
</tr>
<tr>
<td nowrap="nowrap">
<span class="seasonDescHeader">
Episodes:
</span>
<span id="episodeDescSpan" class="seasonDescText">
8
</span>
</td>
</tr>
<tr>
<td nowrap="nowrap">
<span class="seasonDescHeader">
Genre:
</span>
<span class="seasonDescText">
<?php echo str_replace("|"," / ",substr($seriesRow['genre'],1,strlen($seriesRow['genre'])-2));
?>
</span>
</td>
</tr>
</table>
</div>

<!-- season poster panel-->
<div id="seasonPosterDIV" style="padding-top:30px; position:absolute; top:242px; left:57px; width:460px; height:638px; background:url(images/tvSeasonPosterBack.png) no-repeat; text-align:center; vertical-align:middle;">
</div>

<!-- description div -->
<div class="seasonDescTextBody" style="position:absolute; width:1200px; height:700px; top:255px; left:575px; z-index:10;">
<?php echo nl2br($seriesRow['description']);?>
</div>

<!-- overlay -->
<div class="displayOverlay"></div>

<!-- Modal Window Code -->
<?php include('/includes/modalWindow.php'); ?>

</body>
</html>